﻿using System.Configuration;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using VXCompany.SilverlightLogging.Services.ServiceContracts;
 
namespace VXCompany.SilverlightLogging.Services.Business
{
	/// <summary>
    /// Returns client logging settings from service web.config.
    /// </summary>
    public class ClientLogConfig : IConfigurationSectionHandler
    {
        #region Fields

        private static XmlSerializer _ser = new XmlSerializer(typeof(LoggingConfigurationMessage));

        #endregion Fields

        #region Methods

		/// <summary>
		/// Reads config file XmlNode and deserializes it into a LoggingConfigurationMessage instance.
		/// </summary>
		/// <param name="parent"></param>
		/// <param name="configContext"></param>
		/// <param name="section"></param>
		/// <returns></returns>
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            using (StringReader sr = new StringReader(section.InnerXml))
            {
                using (XmlReader xr = XmlReader.Create(sr))
                {
                    LoggingConfigurationMessage msg = (LoggingConfigurationMessage)_ser.Deserialize(xr);
                    return msg;
                }
            }
        }

        #endregion Methods
    }
}